import redis
from omegaconf import OmegaConf

is_pause_key_prefix = 'train:task:isPause:'


def get_redis_client():
    config = OmegaConf.load("application.yaml")
    client = redis.StrictRedis(host=config.redis.host, port=config.redis.port, password=config.redis.password,
                               db=config.redis.db,
                               decode_responses=True)
    try:
        client.ping()
        print("Connected to Redis")
    except redis.ConnectionError:
        print("Could not connect to Redis")

    return client


def is_pause(client, train_task_id):
    key = is_pause_key_prefix + str(train_task_id)
    value = int(client.get(key))
    return value is None or value == -1


def del_pause(client, train_task_id):
    key = is_pause_key_prefix + str(train_task_id)
    client.delete(key)
    print("Deleted pause key")


if __name__ == '__main__':
    client = get_redis_client()
    print(is_pause(client, 1))
    del_pause(client, 1)
